<?php ob_start(); ?>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/standart/dbconnect.php');
function goToLogin(){
	echo '<script LANGUAGE="JavaScript">';
	echo 'parent.location="/login/index.php?rnd="+Math.random();';
	echo '</script>';
}
function setCookies($value){
	setcookie("GEVEZE_ADMIN",$value, time()+(60*60),"/");
}
if(!isset($_COOKIE['GEVEZE_ADMIN'])){
	if(isset($_COOKIE["GEVEZE_ADMIN_USR"]) && isset($_COOKIE["GEVEZE_ADMIN_PASS"])){
		$db = connect();
		$userName = $_COOKIE["GEVEZE_ADMIN_USR"];
		$userPass = $_COOKIE["GEVEZE_ADMIN_PASS"];
		$stmt ="SELECT ID
		FROM ADMIN
		WHERE USERNAME = ?
			AND PASSWORD = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('ss', $userName, $userPass);
		$statement->execute();
		$statement->bind_result($col1);
// 		$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
		if($row = $statement->fetch()){
			setCookies($col1);
		}else{
			goToLogin();
			break;
		}
		$statement->close();
		disconnect($db);
	}else{
		goToLogin();
		break;
	}
}else{
	$db = connect();
	$adminId = $_COOKIE["GEVEZE_ADMIN"];
	$stmt ="SELECT *
		FROM ADMIN
		WHERE ID = $adminId";
	$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	if(!$row = $res->fetch_assoc()){
		goToLogin();
		break;
	}else{
		setCookies($adminId);
	}
	$res->close();
	disconnect($db);
}
?>
<?php ob_end_flush(); ?>